// pages/order/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: 0,
    page: 1,
    kw: '',
    nodata: 0,
    user_info: {},
    status: 1,
    lastpage: 0,
    listData: []

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ extConfig: app.getExtConfigSync() })
    if (options.status) {
      this.data.status = options.status;
      this.setData({ status: options.status });
    }
    app.getUserInfo((user_info) => {
      this.setData({ user_info: user_info });
      this.loadOderList();
    });
    
  },
   
  setsearchkw: function (event) {
    this.data.kw = event.detail.value;
    // console.log(this.data.kw);
  },
  search: function () {
    this.loadOderList(1);
  },

  loadOderList: function (reload) {
    var that = this;

    if (reload == 1) {
      this.setData({
        loading: 0,
        page: 1,
        lastpage: 0,
        listData: [],
      });
    }

    var page = this.data.page;
    if (this.data.loading == 1 || this.data.lastpage == 1) {
      //console.log(this.data);
      return;
    }
    this.data.loading = 1;
    app.api("mall/shop/get", {
      pid: this.data.user_info.user_id,
      page: page,
      limit: 10,
      kw: this.data.kw,
      order: 'id desc',
      store_id:-1,
    }, function (res) {
      that.data.loading = 0;
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      if (res.data.list.length == 0) {
        that.data.lastpage = 1;
      }
      var nodata = 0;
      var list = that.data.listData.concat(res.data.list);
      if (list.length == 0) {
        nodata = 1;
      }
      that.data.page++;
      that.setData({
        listData: list,
        nodata: nodata
      });

    })

  },
  mch_mybank: function (e) {

    var shop_id = e.currentTarget.dataset.shop_id;
    this.data.shop_id = shop_id;
    this.data.shop_name = e.currentTarget.dataset.shop_name;;
    var store_id = e.currentTarget.dataset.store_id;
    var type = e.currentTarget.dataset.type;
    var that = this;
    wx.showActionSheet({
      itemList: ['绑定独立收银盒', '绑定云喇叭', '绑定收款码'],
      success(res) {
        if (res.tapIndex==0){
          that.bindScanBox(shop_id);
        } else if (res.tapIndex == 1 ){
          that.scanCode();
        } else if (res.tapIndex == 2){
          var shop_name = that.data.shop_name;
          var text = "为门店：" + shop_name +"绑定收款码?";
          wx.showModal({
            title: '确认操作',
            content: text,
            success(res) {
              if (res.confirm) {
                that.scanCode();
                }
              }
            })
        }
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })

    
    // var url;
    // if (type == 2) {
    //   url = "/pages/webview/index?url=" + encodeURIComponent("https://wap.bestsmartbee.com/apps/mch/#/store_id/" + store_id + "/pid/" + this.data.user_info.user_id);
    // } else {
    //   url = "/pages/webview/index?url=" + encodeURIComponent("https://wap.bestsmartbee.com/pages/shop_admin/?store_id=" + store_id + "#/bindScanBox/shop_id/" + shop_id);
    // }

    // wx.navigateTo({
    //   url: url,
    // })

  },
  // 微信扫一扫
  scanCode: function () {
    var that = this;
    wx.scanCode({
      success(res) {
        var result = res.result;
        // http
        // https://www.bestsmartbee.com/apiv2/scene/dobind?shop_id=302&id=20197014008517898381631
        
        if (result.indexOf("bestsmartbee.com/apiv2/pay/money/?id=") > 0){
          that.bindCode(result);
        }else if (result.indexOf("bestsmartbee.com") > 0) {
          wx.navigateTo({
            url: '/pages/webview/index?url=' + encodeURIComponent(result),
          })
        }else if (result.indexOf('|') > 0) {
          //  绑定云嗽叭
          that.bindHorn(result);
        }

        console.log(res);
      }, fail(res) {
        console.log(res);
        if (res.errMsg != 'scanCode:fail cancel') {
          app.alert(res.errMsg);
        }

      }
    });
  },
  bindCode:function(url){
    var arg = app.GetRequest(url);
    var id = arg.id;
    app.api("apiv2/scene/dobind", {
      json:1,
      shop_id: this.data.shop_id,
      id:id,
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      app.alert("绑定成功");
    });
  },

  //  绑定云嗽叭
  bindHorn: function (str) {
    var shop_name = this.data.shop_name;
    var shop_id = this.data.shop_id;
    var arr = str.split("|");
    var text = "绑定云嗽叭：" + arr[0] + "到门店：" + shop_name;
    wx.showModal({
      title: '确认操作',
      content: text,
      success(res) {
        if (res.confirm) {
          var data = {
            shop_id: shop_id,
            uuid: arr[0],
            type: '智物联',
          };
          data.method = "POST";
          app.api("apiv2/device_horn/add", data, function (res) {
            if (res.code != 200) {
              app.alert(res.msg);
              return;
            }
            app.alert("云喇叭绑定成功");
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  
  bindScanBox: function (shop_id) { 
    app.api("apiv2/pay_request/bindScanBox/", { shop_id: shop_id }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      var show_qrcode_url = app.data.apiHost + "api/index/urlQRcode?url=" + encodeURIComponent(res.data);
      wx.previewImage({
        current: show_qrcode_url, // 当前显示图片的http链接
        urls: [show_qrcode_url] // 需要预览的图片http链接列表
      })
    });
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.loadOderList(1);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadOderList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})